<%@ include file="shared_header.jsp" %>
<%@ page import="vrp.SaleOrder" %>
<%@ page import="vrp.Delivery" %>
<%@ page import="vrp.Customer" %>
<%@ page import="vrp.Product" %>

<%
	SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "";
%>

<%
	String[] deliveryStatusValue = {Integer.toString(0),Integer.toString(1),Integer.toString(2)};
	String[] deliveryStatusDisplay = {"Processing","Finished","Cancelled"};
    List<Delivery> deliveries;
    
    String error = request.getParameter("error");
    if (error != null) {
    	if (error.equals("quantity")) {
%>
    		<div class="ui-state-error">Delivery quantity must be a positive number.</div>
<%
    	}
    	if (error.equals("inventory")) {
%>
    		<div class="ui-state-error">There is not enough inventory for this delivery.</div>
<%
    	}
    }
%>

	<div style="padding: 0px 10px;">
		<h2>List of Sale Order Deliveries</h2>
		<table style="width: 100%;">
<%
	query = "select from " + Customer.class.getName();
	List<Customer> customers = (List<Customer>) pm.newQuery(query).execute();
	query = "select from " + Product.class.getName();
	List<Product> products = (List<Product>) pm.newQuery(query).execute();

	String urlSaleOrder = request.getParameter("saleOrder"); 
	if (urlSaleOrder != null) {
		long saleOrderID = Integer.parseInt(urlSaleOrder);
		Key saleOrderKey = KeyFactory.createKey(SaleOrder.class.getSimpleName(), saleOrderID);
		query = "select from " + Delivery.class.getName() + " where saleOrder == saleOrderParam parameters String saleOrderParam order by date asc";
		deliveries = (List<Delivery>) pm.newQuery(query).execute(saleOrderKey);
	} else {
		query = "select from " + Delivery.class.getName() + " order by date asc";
		deliveries = (List<Delivery>) pm.newQuery(query).execute();
	}

	if (deliveries.isEmpty()) {
%>
			<tr><td>There has been no delivery for this sale order.</td></tr>
<%
    } else {
%>
			<tr>
				<th>Customer</th>
				<th>Product</th>
				<th>Quantity</th>
				<th>Date</th>
				<th>Status</th>
				<th>Action</th>
			</tr>
<%
        for (Delivery d : deliveries) {
%>
			<tr>
				<td style="text-align: left;"><%= (pm.getObjectById(Customer.class, d.getCustomer())).getName() %></td>
				<td style="text-align: left;"><%= (pm.getObjectById(Product.class, d.getProduct())).getName() %></td>
				<td style="text-align: right;"><%= d.getQuantity() %></td>
				<td style="text-align: center;"><%= sdf.format(d.getDate()) %></td>
				<td style="text-align: center;"><%= writeSwitch(Integer.toString(d.getStatus()),deliveryStatusValue,deliveryStatusDisplay) %></td>
				<td style="text-align:center;">
<%
			if (d.getStatus() == 0) { //PROCESSING
%>
					<form action="/donedelivery" method="post">
						<input type="hidden" name="delivery" value="<%= d.getKey().getId() %>"/>
						<input type="hidden" name="urlSaleOrder" value="<%= urlSaleOrder %>"/>
						<input type="submit" value="Delivery Done"/>
					</form>
					<form action="/canceldelivery" method="post">
						<input type="hidden" name="delivery" value="<%= d.getKey().getId() %>"/>
						<input type="hidden" name="urlSaleOrder" value="<%= urlSaleOrder %>"/>
						<input type="submit" value="Cancel Delivery"/>
					</form>
					<input type="button" value="Edit" class="buttonedit"/>
<%				
			}
			if (d.getStatus() == 2) { //CANCELLED
%>
					<form action="/reenabledelivery" method="post">
						<input type="hidden" name="delivery" value="<%= d.getKey().getId() %>"/>
						<input type="hidden" name="urlSaleOrder" value="<%= urlSaleOrder %>"/>
						<input type="submit" value="Reenable Delivery"/>
					</form>
					<input type="button" value="Edit" class="buttonedit"/>
<%				
			}
%>
					<input type="button" value="Delete" class="buttondelete"/>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="6">
					<form action="/editdelivery" method="post">
						<div><b>Edit Delivery</b></div>
			    		<div>Customer:&nbsp;
			    			<select name="customer">
<%
        					for (Customer c : customers) {
%>			    			
							 	<option value="<%= c.getKey().getId() %>" <%= (c.getKey().getId()==d.getCustomer().getId())?"selected":"" %>><%= c.getName() %></option>
<%
        					}
%>							</select>
			    		</div>
			    		<div>Product:&nbsp;
			    			<select name="product">
<%
        					for (Product p : products) {
%>			    			
							 	<option value="<%= p.getKey().getId() %>" <%= (p.getKey().getId()==d.getProduct().getId())?"selected":"" %>><%= p.getName() %></option>
<%
        					}
%>							</select>
			    		</div>
			    		<div>Quantity:&nbsp;<input type="text" name="quantity" value="<%= d.getQuantity() %>" /></div>
			    		<div>Date:&nbsp;<input type="text" name="date" value="<%= sdf.format(d.getDate()) %>" class="datePicker" /></div>
			    		<input type="hidden" name="delivery" value="<%= d.getKey().getId() %>"/>
						<input type="hidden" name="urlSaleOrder" value="<%= urlSaleOrder %>"/>
						<input type="submit" value="Edit" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
			<tr style="display: none;">
				<td colspan="5">
					<form action="/deletedelivery" method="post">
						<div><b>Delete Delivery</b></div>
			    		<div>This action would delete the above delivery permanently. Proceed?</div>
						<input type="hidden" name="delivery" value="<%= d.getKey().getId() %>"/>
						<input type="hidden" name="urlSaleOrder" value="<%= urlSaleOrder %>"/>
						<input type="submit" value="Delete" />
						<input type="button" value="Cancel" class="buttoncancel" />
					</form>
				</td>
			</tr>
<%
        }
    }
%>
		</table>
	</div>

<%
    pm.close();
%>

<%@ include file="shared_footer.jsp" %>